package threadDemo1.concurrent;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-02-27
 * Time: 16:19
 */
public class ThreadDemo {
    public static long result = 0L;
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(()->{
            long temp = 0L;
            for (long i = 1; i <= 5_0000_0000L; i++) {
                temp += i;
            }
            result += temp;
        });

        Thread thread2 = new Thread(()->{
            long temp = 0L;
            for (long i = 5_0000_0001L; i <= 10_0000_0000L; i++) {
                temp += i;
            }
            result += temp;
        });

        long begin = System.currentTimeMillis();

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        long end = System.currentTimeMillis();

        System.out.println("result = " + result);
        System.out.println("并行运算 = " + (end - begin));
    }
//    public static long result = 0L;
//
//    public static void main(String[] args) {
//        long start = System.currentTimeMillis();
//        for (long i = 1; i <= 10_0000_0000; i++) {
//            result += i;
//        }
//        long end = System.currentTimeMillis();
//
//        System.out.println("result = " + result);
//        System.out.println("串行运算 = " + (end - start));
//    }
}
